//
//  AntiTheftModeVC.m
//  Toyota
//
//  Created by ld on 2024/6/26.
//

#import "AntiTheftModeVC.h"
#import "AppointmentAntiTheftVC.h"
#import "PopUpTipsView.h"
#import <LSTPopView.h>
#import "Definition.h"
#import "Helper.h"
@interface AntiTheftModeVC () {
    UIImageView * imageView;
    UILabel * contentLab;
    
    BOOL statusIsSuccess;
}
@property (nonatomic, strong) UIButton * openBtn;

@end

@implementation AntiTheftModeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kColorFFFFFF;
    self.title = @"防盗模式";
    statusIsSuccess = NO;
    
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"预约" style:UIBarButtonItemStylePlain target:self action:@selector(actionRightBar:)];
    [rightBarButtonItem setTitleTextAttributes:@{
        NSFontAttributeName:fontSize(15),
        NSForegroundColorAttributeName:kColor141414
    } forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
    [self initUI];
    // Do any additional setup after loading the view.
}

-(void)initUI {
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake((ScreenWidth - 200) / 2., 60, 200, 200)];
    imageView.image = [UIImage imageNamed:@"未开启"];
    [self.view addSubview:imageView];
    
    contentLab = [[UILabel alloc]initWithFrame:CGRectMake(20, imageView.customBottom + CYScalXH(40), ScreenWidth - 40, 20)];
    contentLab.text = @"为防止汽车被盗，可开启发动机镇止功能";
    contentLab.textColor = kColor141414;
    contentLab.font = fontBold(14);
    contentLab.numberOfLines = 0;
    contentLab.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:contentLab];
    
    //按钮
    _openBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _openBtn.frame = CGRectMake((ScreenWidth - 260) / 2., contentLab.customBottom + CYScalXH(60), 260, 40);
    [_openBtn setTitle:@"开启防盗" forState:UIControlStateNormal];
    [_openBtn setTitle:@"防盗开启中" forState:(UIControlStateSelected)];
    [_openBtn setBackgroundImage:[UIImage imageWithColor:kColor141414] forState:(UIControlStateNormal)];
    [_openBtn setBackgroundImage:[UIImage imageWithColor:kColorC2C2C2] forState:(UIControlStateSelected)];
    _openBtn.titleLabel.font = fontSize(15);
    _openBtn.clipsToBounds = YES;
    _openBtn.layer.cornerRadius = CYScalW(20);
    [_openBtn addTarget:self action:@selector(openBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_openBtn];
}

- (void)actionRightBar:(UIBarButtonItem *)sender {
    AppointmentAntiTheftVC * vc = [[AppointmentAntiTheftVC alloc]init];
    [self.navigationController pushViewController:vc animated:NO];
}

-(void)openBtnClick:(UIButton *)sender {
    NSString * titleStr = @"";
    NSString * contentStr = @"";
    if ([sender.titleLabel.text isEqualToString:@"开启防盗"]) {
        titleStr = @"确认要开启发动机锁定吗？";
        contentStr = @"";
    }else  if ([sender.titleLabel.text isEqualToString:@"关闭防盗模式"]){
        titleStr = @"关闭发动机锁定";
        contentStr = @"爱车已开启防盗模式，确定关闭?";
    }
    
    PopUpTipsView * popUpView = [[PopUpTipsView alloc]initWithFrame:CGRectMake(0, 0, CYScalW(280), 200)];
    [popUpView tipsWithTitle:titleStr andContent:contentStr andContentAlignment:NSTextAlignmentCenter andBtnTitleArr:@[@"取消", @"确定"] andbtnColorArr:@[kColor141414, [UIColor whiteColor]] andBtnBgColorArr:@[[UIColor whiteColor], kColor141414]];
    LSTPopView *popView = [LSTPopView initWithCustomView:popUpView popStyle:LSTPopStyleFade dismissStyle:LSTDismissStyleFade];
    __block LSTPopView *blockFooter = popView;
    popView.popDuration = 0.5;
    popView.hemStyle = LSTHemStyleCenter;
    popView.dismissDuration = 0.5;
    popView.bgAlpha = 0.5;
    [popView pop];
    popView.bgClickBlock = ^{
        [blockFooter dismiss];
    };
    popUpView.customHeight = popUpView.bgV.customBottom;
    popUpView.choicActionBlock = ^(NSString * _Nonnull choiceAction) {
        if ([choiceAction isEqualToString:@"取消"]) {
            [blockFooter dismiss];
        }else if ([choiceAction isEqualToString:@"确定"]) {
            [blockFooter dismiss];
            if ([sender.titleLabel.text isEqualToString:@"关闭防盗模式"]){
                self.openBtn.selected = YES;
                self.openBtn.enabled = NO;
                self->contentLab.text = @"中心已启动远程防盗，您的发动机已被锁定\n如需解锁请联系 400-818-9919";
                self->contentLab.attributedText = [self->contentLab setupAttributeString:self->contentLab.text andFont:fontBold(14) andColor:kColor141414 rangeText:@"如需解锁请联系" rangeTextColor:kColor686868 rangeFont:fontSize(13) andTextAlignment:(NSTextAlignmentCenter) rangeLineHeight:15];
                [self->contentLab addAttributeActionWithStrings:@[@"400-818-9919"] tapTargetAction:^(NSString *string, NSRange range, NSInteger index) {
                    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", @"400-818-9919"]]];
                }];
                [self->contentLab sizeToFit];
                self->contentLab.customWidth = ScreenWidth - 40;
                self.openBtn.customY = self->contentLab.customBottom + CYScalXH(60);
            }else {
                self.openBtn.selected = YES;
                [self.openBtn setTitle:@"防盗开启中" forState:(UIControlStateNormal)];
                self.openBtn.enabled = NO;
                self->imageView.image = [UIImage imageNamed:@"开启中"];
                self->contentLab.text = @"指令已下发";
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    if (self->statusIsSuccess) {
                        self.openBtn.selected = NO;
                        self.openBtn.enabled = YES;
                        [self.openBtn setTitle:@"关闭防盗模式" forState:(UIControlStateNormal)];
                        self->imageView.image = [UIImage imageNamed:@"已开启"];
                        self->contentLab.text = @"您的发动机已锁定，如需解锁请点击按钮";
                    }else {
                        self->statusIsSuccess = YES;
                        [self failInSend];
                        self.openBtn.selected = NO;
                        self.openBtn.enabled = YES;
                        [self.openBtn setTitle:@"开启防盗" forState:UIControlStateNormal];
                        self->imageView.image = [UIImage imageNamed:@"未开启"];
                        self->contentLab.text = @"为防止汽车被盗，可开启发动机镇止功能";
                    }
                });
            }
        }
    };
}

-(void)failInSend {
    PopUpTipsView * popUpView = [[PopUpTipsView alloc]initWithFrame:CGRectMake(0, 0, CYScalW(280), 200)];
    [popUpView tipsWithTitle:@"发送失败" andContent:@"指令发送失败，请稍后重试" andContentAlignment:NSTextAlignmentCenter andBtnTitleArr:@[@"好的"] andbtnColorArr:@[[UIColor whiteColor]] andBtnBgColorArr:@[kColor141414]];
    LSTPopView *popView = [LSTPopView initWithCustomView:popUpView popStyle:LSTPopStyleFade dismissStyle:LSTDismissStyleFade];
    __block LSTPopView *blockFooter = popView;
    popView.popDuration = 0.5;
    popView.hemStyle = LSTHemStyleCenter;
    popView.dismissDuration = 0.5;
    popView.bgAlpha = 0.5;
    [popView pop];
    popView.bgClickBlock = ^{
        [blockFooter dismiss];
    };
    popUpView.customHeight = popUpView.bgV.customBottom;
    popUpView.choicActionBlock = ^(NSString * _Nonnull choiceAction) {
        if ([choiceAction isEqualToString:@"好的"]) {
            [blockFooter dismiss];
        }
    };
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
